<template>
  <div>
    <!-- 地址  -->
    <div class="address" @click="$router.push('/aList?id=' + address.id)">
      <!-- 有地址  -->
      <div v-if="address.id">
        <h3 class="text-primary h2">
          {{ address.username }} {{ address.userphone }}
        </h3>
        <div>
          <van-tag type="danger" v-if="address.status == 1">默认</van-tag>
          <van-tag type="danger" v-if="address.label">{{
            address.label
          }}</van-tag>
          <span class="p">{{ address.location }}</span>
        </div>
      </div>
      <!-- 无地址 -->
      <van-button v-else>点击完善收货地址</van-button>
    </div>
    <!-- 购物车  -->
    <van-card
      v-for="item in list"
      :key="item.id"
      :num="item.num"
      :price="item.num * item.price"
      :desc="'￥' + item.price"
      :title="item.goodsname"
      :thumb="$pre + item.img"
    />
    <!-- 结算的栏目 -->
    <van-submit-bar
      :price="totalPrice * 100"
      button-text="提交订单"
      @submit="onSubmit"
    />
  </div>
</template>

<script>
import { Toast } from "vant";
import { reqCartlist, reqAddresslist,reqOrderadd } from "../../http/http";
export default {
  data() {
    return {
      list: [],
      totalNum: "",
      totalPrice: "",
      shopID: [],
      address: {},
    };
  },
  mounted() {
    let { totalNum, totalPrice, shopID } = JSON.parse(
      sessionStorage.getItem("orderInfo")
    );
    this.totalNum = totalNum;
    this.totalPrice = totalPrice;
    this.shopID = shopID.split(",");
    this.getGoodsList();
    this.getUserAddress();
  },
  methods: {
    //提交订单
    async onSubmit() {
      if (!this.address.username) {
        Toast("请填写收货地址");
        return;
      }
      let { uid } = JSON.parse(localStorage.getItem("isLogin"));
      let { totalNum, totalPrice, shopID } = JSON.parse(
      sessionStorage.getItem("orderInfo")
    );
      let {data:res} = await reqOrderadd({uid,countmoney:totalPrice,countnumber:totalNum,addressid:this.address.id,idstr:shopID});
      if(res.code == 200){
        Toast('提交成功');
        sessionStorage.removeItem('orderInfo');
        this.$router.replace("/pay?orderId="+res.list.outTradeNo)
      }      
    },
    //获取要购买商品的信息
    getGoodsList() {
      let { uid } = JSON.parse(localStorage.getItem("isLogin"));
      this.shopID.forEach(async (item) => {
        let { data: res } = await reqCartlist({ uid, id: item });
        this.list.push(res.list[0]);
      });
    },
    //获取用户地址
    async getUserAddress() {
      let { uid } = JSON.parse(localStorage.getItem("isLogin"));
      if (this.$route.query.addressId) {
        //有addressId，请求相应数据
        let { data: res } = await reqAddresslist({
          uid,
          id: this.$route.query.addressId,
        });
        if (res.code == 200) {
          this.address =res.list[0];
        }
      } else {
        //没有addressId,请求默认
        let { data: res } = await reqAddresslist({ uid, status: 1 });
        let list = res.list ? res.list : [];
        if (list.length == 0) {
          this.address = {};
        }
        if (list.length > 0) {
          let obj = list[0];
          if (obj.status === 1) {
            this.address = obj;
          } else {
            this.address = {};
          }
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.van-card__title {
  line-height: 1em;
}
.address {
  padding: 20px;
  background: #999;
  text-align: center;
}
</style>